package com.weiaibenpao.demo.chislim.jpush;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

import com.weiaibenpao.demo.chislim.ui.MainActivity;

import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

public class TestActivity extends Activity {


    String myValue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        TextView tv = new TextView(this);
        tv.setText("用户自定义打开的Activity");
        Intent intent = getIntent();
        if (null != intent) {
	        Bundle bundle = getIntent().getExtras();
	        String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
	        String content = bundle.getString(JPushInterface.EXTRA_ALERT);

            String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);          //获取键值对
            //获取键值对中的值
            processCustomMessage(this, bundle);
            JSONObject extrasJson = null;
            try {
                extrasJson = new JSONObject(extras);
                myValue = extrasJson.optString("url");
                bundle.putString("url",myValue);
                Log.i("ceshi",myValue);
            } catch (JSONException e) {
                e.printStackTrace();
            }

	        tv.setText("Title : " + title + "  " + "Content : " + content + "     url    " + myValue);
            Log.i("接收到的推送消息","Title : " + title + "  " + "Content : " + content + "url" + myValue);
        }
        addContentView(tv, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    }

    //send msg to MainActivity
    private void processCustomMessage(Context context, Bundle bundle) {
        if (MainActivity.isForeground) {
            String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
            String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
            Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);
            msgIntent.putExtra(MainActivity.KEY_MESSAGE, message);
            if (!ExampleUtil.isEmpty(extras)) {
                try {
                    JSONObject extraJson = new JSONObject(extras);
                    if (null != extraJson && extraJson.length() > 0) {
                        msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);
                    }
                } catch (JSONException e) {

                }
            }
            context.sendBroadcast(msgIntent);
        }
    }
}
